package org.summerframework.component.upload.fastdfs;

import org.summerframework.component.upload.AbstractUploadServerService;
import org.summerframework.component.upload.FileUploadServerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @author 石超
 * @version v1.0.0
 */
@Slf4j
public class FastDFSUploadServerServiceImpl extends AbstractUploadServerService implements FileUploadServerService {
    @Resource
    private FastdfsTemplate fastdfsTemplate;

    @Override
    public String uploadFile(InputStream is, String fileName) {
        return fastdfsTemplate.uploadFileByInputStream(is, FilenameUtils.getExtension(fileName), null).getFileId();
    }

    @Override
    public byte[] getFileBytes(String path) {
        try {
            return fastdfsTemplate.downLoadFile(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AbstractUploadServerService.EMPTY_BYTES;
        }
    }

    @Override
    public InputStream getInputStream(String path) {
        return new ByteArrayInputStream(getFileBytes(path));
    }

    @Override
    public void deleteFile(String path) {
        try {
            fastdfsTemplate.deleteFile(path);
        } catch (Exception e) {
            log.warn("删除文件是不", e);
        }
    }
}
